package com.paw.oauth2.server.service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Service;

/**
 * @author lvzihai
 * @date 2021/7/14
 **/
@Service
public class DefaultClientDetailService implements ClientDetailsService
{

  @Autowired
  private PasswordEncoder passwordEncoder;

  @Override
  public ClientDetails loadClientByClientId (String clientId) throws ClientRegistrationException {
    String grantType = "password,authorization_code,client_credentials,implicit,refresh_token";
    BaseClientDetails clientDetails =  new BaseClientDetails(clientId,null,"all",grantType,"USER");
    Set<String> urls = new HashSet<>();
//    urls.add("http://localhost:8060/");
//    urls.add("http://localhost:8060/uaa");
    urls.add("http://localhost:8082/marshall/login");
    urls.add("http://localhost:8083/zuma/login");
    urls.add("http://localhost:8082/login");
    urls.add("http://localhost:8083/login");
    urls.add("http://localhost:8086/login");
    urls.add("http://127.0.0.1:8086/login");
    clientDetails.setRegisteredRedirectUri(urls);
//    clientDetails.setAutoApproveScopes(Arrays.asList("all","read","write"));
    // true 所有都是
    clientDetails.setAutoApproveScopes(Arrays.asList("true"));
    String password = passwordEncoder.encode("123456");
    clientDetails.setClientSecret(password);
    return clientDetails;
  }
}
